use crate::{
    app::{CompMode, DrawResult},
    complex::Complex,
    create_std_mode,
};

create_std_mode! {Mandel, Julia, Pavano}

impl CompMode for Mandel {
    fn comp(&self, c: Complex) -> DrawResult {
        let mut t = comp!(self.offset_x, self.offset_y);
        for _ in 0..self.iter_times {
            t = t.pow(self.pow) + c;
            if t.abs() > self.conver_val {
                return DrawResult::Fail;
            }
        }

        DrawResult::Success
    }
}

impl CompMode for Julia {
    fn comp(&self, mut c: Complex) -> DrawResult {
        let t = comp!(self.offset_x, self.offset_y);
        for _ in 0..self.iter_times {
            c = c.pow(self.pow) + t;
            if c.abs() > self.conver_val {
                return DrawResult::Fail;
            }
        }

        DrawResult::Success
    }
}

impl CompMode for Pavano {
    fn comp(&self, c: Complex) -> DrawResult {
        let mut t = comp!(self.offset_x, self.offset_y);
        for _ in 0..self.iter_times {
            t = comp!(t.img(), t.real()).pow(self.pow) + c;
            if t.abs() > self.conver_val {
                return DrawResult::Fail;
            }
        }

        DrawResult::Success
    }
}

// 自定义部分
create_std_mode! { Xhonato }

impl CompMode for Xhonato {
    fn comp(&self, c: Complex) -> DrawResult {
        let mut t = comp!(self.offset_x, self.offset_y);
        for _ in 0..self.iter_times {
            t = comp!(t.real() + t.img(), t.real() - t.img()).pow(self.pow) + c;
            if t.abs() > self.conver_val {
                return DrawResult::Fail;
            }
        }

        DrawResult::Success
    }
}
